package com.moguhoutai.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.moguhoutai.biz.ResourceBiz;
import com.moguhoutai.model.Resources;
import com.moguhoutai.util.BusinessException;
@Controller
@RequestMapping("/resource")
public class ResourcesController {
    @Resource
	private ResourceBiz reBiz;
	
    @RequestMapping("/list")
   	public String list(Map<String, Object> request){
   		try {
   			long count = reBiz.findCount();
   			List<Resources> list = reBiz.findAll();
   			request.put("totalCount", count);
   			request.put("list", list);
   		} catch (BusinessException e) {
   			e.printStackTrace();
   		}
   		return "/resource/list";
   	}
	   @RequestMapping("/preAdd")
		public String preAdd(Map<String, Object> request){
			try {
				request.put("res", new Resources());
				List<Resources> list = reBiz.findAll();
				request.put("list", list);
			} catch (BusinessException e) {
				e.printStackTrace();
			}
			return "/resource/add";
		}
    
	   
	   @RequestMapping("/add")
		public String add(Resources res){
		       try {
				reBiz.save(res);
			} catch (BusinessException e) {
				e.printStackTrace();
			}
			return "redirect:list";
		}
	   
    
    @RequestMapping("/delete/{resId}")
    public String  delete(@PathVariable String resId){
    	try {
    		Resources res =reBiz.findById(resId);
    		if(res==null){
    			return "redirect:list";
    		}
    		reBiz.delete(res);
		} catch (Exception e) {
			e.printStackTrace();
			
		}
    	return "redirect:list";
    }
    
	@RequestMapping("/preUpdate/{resId}")
	public String preUpdate(@PathVariable String resId, Map<String, Object> request){
		try {
		      Resources res = reBiz.findById(resId);
		} catch (BusinessException e) {
			e.printStackTrace();
		}
		return "/resource/update";
	}
    
	@RequestMapping("/update")
	public String update(Resources res){
		try {
			reBiz.update(res);
		} catch (BusinessException e) {
			e.printStackTrace();
		}
		return "redirect:list";
	}
}
